//
//  XSJScrollView.m
//  XSJScrollView
//
//  Created by xalo on 16/6/22.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "XSJScrollView.h"

#define KWidth self.frame.size.width
#define KHeight self.frame.size.height
@interface XSJScrollView ()<UIScrollViewDelegate>
//滚动视图
@property (nonatomic, strong)UIScrollView *scrollView;
//左图片
@property (nonatomic, strong)UIImageView *leftImageView;
//右图片
@property (nonatomic, strong)UIImageView *rightImageView;
//图片数组
@property (nonatomic, strong)NSArray *imageArray;
//当前下标
@property (nonatomic, assign)NSInteger index;
//小圆点
@property (nonatomic, strong)UIPageControl *pageControl;
//定时器
@property (nonatomic, strong)NSTimer *timer;

@end

@implementation XSJScrollView

#pragma mark -------------------懒加载----------------------
- (NSTimer *)timer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    return _timer;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        //创建滚动视图及属性
        _scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
        _scrollView.contentSize = CGSizeMake(KWidth * 2, KHeight);
        _scrollView.bounces = NO;
        _scrollView.bouncesZoom = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

- (UIPageControl *)pageControl{
    if (!_pageControl) {
        //创建小圆点
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(kWidth/2 - 50, KHeight - 50, 100, 50)];
        _pageControl.numberOfPages = self.imageArray.count;
    }
    return _pageControl;
}

- (UIImageView *)leftImageView{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc] initWithFrame:self.frame];
        [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]]];

    }
    return _leftImageView;
}

- (UIImageView *)rightImageView{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWidth, 0, KWidth, KHeight)];
        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
    }
    return _rightImageView;
}


- (instancetype)initWithFrame:(CGRect)frame
                   imageArray:(NSArray *)imageArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.index = 1;
        //给数组赋值
        self.imageArray = imageArray;
        //设置视图
        [self createUI];
    }
    return self;
}

- (void)createUI{
    [self.scrollView addSubview:self.leftImageView];
    [self.scrollView addSubview:self.rightImageView];
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.timer fire];
    });
}


//定时器事件
- (void)timerAction{
    [self.scrollView setContentOffset:CGPointMake(KWidth, 0) animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == KWidth) {
        scrollView.contentOffset = CGPointZero;
        self.pageControl.currentPage = self.index;
        self.index++;
        if (self.index == self.imageArray.count) {
            self.index = 0;
        }
        self.leftImageView.image = self.rightImageView.image;
        [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
    }
}










@end
